/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * fastAddSpravka_Item.java
 *
 * Created on Jan 25, 2009, 11:37:56 PM
 */

package GUI.other;

import GUI.AddToSklad;
import functions.Dogy;
import functions.Drugs;
import functions.Items;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import model.Drug;
import model.Item;

/**
 *
 * @author JArik
 */
public class fastItemAdd extends fastAdd {

    /** Creates new form fastAddSpravka_Item */
    private Item toRec;
    private AddToSklad parent;
    private String itemText, descrText;
    public fastItemAdd(Item rec,AddToSklad nParent) {
        super(nParent);
        initComponents();
        parent = nParent;
        toRec = rec;
        ArrayList<String> types;
        try {
            types = Drugs.getTypes();
            types.add(0, "Лекарство");
            types.add(1, "Другое");
            itemType.setModel(new javax.swing.DefaultComboBoxModel(types.toArray()));
        } catch (SQLException ex) {
            
        }
        itemText = "Название";
        descrText = "Описание предмета";
        if(!parent.getActiveFieldText().equals(""))
            itemName.setText(parent.getActiveFieldText());
        else
        {
          itemName.setText(itemText);
          itemName.setForeground(Color.gray);
        }
        descrField.setText(descrText);
        descrField.setForeground(Color.gray);

       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        itemName = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        itemType = new javax.swing.JComboBox();
        addButton = new javax.swing.JButton();
        descrField = new javax.swing.JTextField();

        setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true));

        jLabel1.setText("Быстрое добавление в каталог");

        itemName.setText("Name");
        itemName.setNextFocusableComponent(descrField);
        itemName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                itemNameFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                itemNameFocusLost(evt);
            }
        });
        itemName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                itemNameKeyReleased(evt);
            }
        });

        jButton1.setText("Закрыть");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        itemType.setEditable(true);
        itemType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Лекарство", "Другое" }));
        itemType.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                itemTypeMouseClicked(evt);
            }
        });
        itemType.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                itemTypeItemStateChanged(evt);
            }
        });
        JTextField field = (JTextField)itemType.getEditor().getEditorComponent();
        field.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                itemTypeMouseClicked(evt);
            }
        });

        addButton.setText("Добавить");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        descrField.setText("Описание");
        descrField.setNextFocusableComponent(addButton);
        descrField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                descrFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                descrFieldFocusLost(evt);
            }
        });
        descrField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                descrFieldKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jButton1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(addButton))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(itemName, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(itemType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(descrField, javax.swing.GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(itemName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(itemType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(descrField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        parent.disposeDialog();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
       try
       {
         if(itemName.getText().equals(itemText) || itemName.getText().equals(""))
             throw new Exception("Введите название предмета.");
         Drug drug = new Drug();
         drug.setName(itemName.getText());
         int index = itemType.getSelectedIndex();
       if(index == 1 )
       {
            try {
                int i = Items.addToCatalog((Item)drug);
                Item item = (Item)drug;
                item.setId(i);
                parent.setActiveFieldText(item);
                parent.disposeDialog();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
       }
       else
       {
           if(index == 0)
           drug.setType("");
           else
               drug.setType(itemType.getSelectedItem().toString());
            try {
                // TODO check for duplicates
                if(!descrField.getText().equals(descrText))
                   drug.setDescr(descrField.getText());
                int i = Drugs.addToCatalog(drug);
                drug.setId(i);
                parent.setActiveFieldText(drug);
                parent.disposeDialog();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
       }
       } // try
       catch(Exception e)
       {

       }
}//GEN-LAST:event_addButtonActionPerformed

    private void itemNameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_itemNameFocusGained
       Dogy.focusGained(itemName, itemText);
    }//GEN-LAST:event_itemNameFocusGained

    private void itemNameFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_itemNameFocusLost
        Dogy.focusLost(itemName, itemText);
    }//GEN-LAST:event_itemNameFocusLost

    private void descrFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_descrFieldFocusGained
        Dogy.focusGained(descrField, descrText);
}//GEN-LAST:event_descrFieldFocusGained

    private void descrFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_descrFieldFocusLost
        Dogy.focusLost(descrField, descrText);
}//GEN-LAST:event_descrFieldFocusLost

    private void itemTypeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_itemTypeMouseClicked
        JTextField field = (JTextField)itemType.getEditor().getEditorComponent();
        field.setSelectionStart(0);
        field.setSelectionEnd(field.getText().length());
    }//GEN-LAST:event_itemTypeMouseClicked

    private void itemTypeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_itemTypeItemStateChanged
        JTextField field = (JTextField)itemType.getEditor().getEditorComponent();
        field.setSelectionStart(0);
        field.setSelectionEnd(field.getText().length());
    }//GEN-LAST:event_itemTypeItemStateChanged

    private void itemNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_itemNameKeyReleased
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
            addButtonActionPerformed(null);
    }//GEN-LAST:event_itemNameKeyReleased

    private void descrFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_descrFieldKeyReleased
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
            addButtonActionPerformed(null);
    }//GEN-LAST:event_descrFieldKeyReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JTextField descrField;
    private javax.swing.JTextField itemName;
    private javax.swing.JComboBox itemType;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables

}
